<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" append-to-body @close="hClose">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    {{ curId }}
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { assignRoles, getRolesEnabled } from '@/api/role'
import { getEmployeeDetail } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      roleList: [], // 所有的角色数据
      roleIds: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async hSave() {
      // 调用接口
      await assignRoles({ id: this.curId, roleIds: this.roleIds })

      this.$message.success('分配角色成功')

      this.hClose()
    },
    async getRolesById() {
      // 请求接口，获取当前员工的角色信息
      const res = await getEmployeeDetail(this.curId)
      console.log('获取当前员工的角色信息', res)

      // 回填
      this.roleIds = res.data.roleIds
    },
    hClose() {
      this.$emit('update:showRoleDialog', false)
    },
    async loadRoles() {
      const res = await getRolesEnabled()
      console.log(res)
      this.roleList = res.data
    }
  }
}
</script>
